package system;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/studentTestList")
public class studentTestList extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session=req.getSession();
        String userid=(String)session.getAttribute("userid");
        
        sqlOperation sql = new sqlOperation();

        int count = sql.getCount("select count(*) from student_test where student_id='"+userid+"'");
        String result = "{\"count\":\""+count+"\""; 
        
        String[] list = sql.getQuerySet("select test_id from student_test where student_id='"+userid+"'",count);
        
        for(int i=0;i<count;i++){
			result+=",\"testId"+String.valueOf(i+1)+"\":\""+list[i]+"\",\"testName"+String.valueOf(i+1)+"\":\""+(sql.getQuerySet("select test_name from testing where test_id='"+list[i]+"'", 1))[0]+"\",\"isFinished"+String.valueOf(i+1)+"\":\""+(sql.getQuerySet("select isFinished from student_test where test_id='"+list[i]+"' and student_id='"+userid+"'", 1))[0]+"\",\"operaterName"+String.valueOf(i+1)+"\":\""+(sql.getQuerySet("select user_name from user_inf,testing where testing.test_id='"+list[i]+"' and testing.operater_id=user_inf.user_id",1))[0]+"\"";
		}
        
        result +="}";
        
        //System.out.print(result);
        
        resp.setContentType("application/json;charset=UTF-8");
		PrintWriter out = resp.getWriter();		
		out.write(result);
	}
    
}
